// pages/focus/index.js
var util = require('../../../utils/util');
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    id: '',
    questionList: '', //精选问题列表
    showloading: true, //加载中loading
    showfinish: false, //数据加载完成loading
    flag: false,
    zuoze: '',
    zuozelist: '',
    articleList:'',
    page: 1,
    num: null,
    type:'',
    value:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    if (util.iflogin()) {
      var userinfo = wx.getStorageSync("USER")
      this.setData({
        flag: true,
        id: userinfo.id
      })

    }
  this.getarticleListdata()

  },

  getarticleListdata:function(){
    var _this = this
  //推荐的文章
  wx.request({
    url: `${BASE_URL}/strategy/listdata.html?page=1&size=10`,
    method:'POST',
    header: {
      'Content-Type': 'application/json' //GET方式是这个
    },
    success(res) {
      console.log(res)
      // res.data.data.forEach(function (item, index) {
      //   if (item.banner) {
      //     item.cover = item.banner
      //   }
      // })
      _this.setData({
        articleList: res.data.row,
        showloading: false,
        num: res.data.num,
        type:'normal',
        page: 1,
      })
    }
  })
  },

 //搜索框绑定的函数
 onchange(e) {
  this.setData({
    value: e.detail,
  });
  if(e.detail==''){
  this.getarticleListdata()

  }
 },
 searchStrategy:function(){
  var _this = this;
  if(_this.data.value!==''){
  wx.request({
    url: `${BASE_URL}/strategy/listdata.html?search=${_this.data.value}&page=1&size=10`,
    method: "POST",
    data:{
      type:'xiaochengxv'
    },
    header: {
      'Content-type': 'application/x-www-form-urlencoded'
      // 'Content-Type': 'application/json'
    },
    success(res) {
      console.log(res)
      _this.setData({
        articleList: res.data.row,
        showloading: false,
        num: res.data.num,
        type:'search',
        page: 1,
      })
    }
  })
  }
 },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: util.throttle(function (e) {
    var _this = this
    console.log(_this.data.articleList.length, _this.data.num)
    if (_this.data.articleList.length !== _this.data.num) {
      _this.setData({
        page: _this.data.page + 1,
        showloading: true,
        showfinish: false,
      })
    } else {
      _this.setData({
        showloading: false,
        showfinish: true,
      })
    }
    if (_this.data.showloading) {
      setTimeout(function () {
        if(_this.data.type=='normal'){
          wx.request({
            url: `${BASE_URL}/strategy/listdata.html?page=${_this.data.page}&size=10`,
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "POST",
            success(res) {
              console.log(res)
              console.log(_this.data.page)
              _this.setData({
                showloading: false,
                articleList: _this.data.articleList.concat(res.data.row),
              })
            }
          })
        }else{
          console.log('suosou')
          wx.request({
            url: `${BASE_URL}/strategy/listdata.html?search=${_this.data.value}&page=${_this.data.page}&size=10`,
            header: {
              'Content-Type': 'application/json', //GET方式是这个
            },
            method: "POST",
            success(res) {
              console.log(res,'sousuo')
              console.log(_this.data.page)
              _this.setData({
                showloading: false,
                articleList: _this.data.articleList.concat(res.data.row),
              })
            }
          })
        }
      }, 1000)
    }
    console.log("触底了")
  }, 2000),

 //跳转文章详情页
 articleDetail: function (e) {
  var id = e.currentTarget.dataset.id 
  wx.navigateTo({
    url: `/views/txtdetail/index?id=${id}`
  })
},
})